﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _skinsajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }
            Admin_Load("0000", "json");
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxGetList":
                    ajaxGetList();
                    break;
                case "ajaxDel":
                    ajaxDel();
                    break;
                case "checkname":
                    ajaxCheckName();
                    break;
                case "ajaxCreateGlobalCSS":
                    ajaxCreateGlobalCSS();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxCheckName()
        {
            if (q("id") == "0")
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title";
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_normal_skins"))
                    this._response = JsonResult(0, "不可添加");
                else
                    this._response = JsonResult(1, "可以添加");
            }
            else
            {
                doh.Reset();
                doh.ConditionExpress = "title=@title and id<>" + q("id");
                doh.AddConditionParameter("@title", q("txtTitle"));
                if (doh.Exist("jcms_normal_skins"))
                    this._response = JsonResult(0, "不可修改");
                else
                    this._response = JsonResult(1, "可以修改");
            }
        }
        private void ajaxGetList()
        {
            doh.Reset();
            doh.SqlCmd = "Select [ID],[Title],[Source],[BodyWidth],[LeftWidth],[RightWidth] FROM [jcms_normal_skins] ORDER BY id desc";
            DataTable dt = doh.GetDataTable();
            this._response = "{result :\"1\",returnval :\"操作成功\"," + Jumbot.Common.JsonHelp.DtToSON(dt) + "}";
        }
        private void ajaxDel()
        {
            string sId = f("id");
            doh.Reset();
            doh.ConditionExpress = "skinid=@id";
            doh.AddConditionParameter("@id", sId);
            if (doh.Exist("jcms_normal_template"))
            {
                this._response = JsonResult(0, "有模板选择了该皮肤，不能删除");
                return;
            }
            doh.Reset();
            doh.ConditionExpress = "id=@id";
            doh.AddConditionParameter("@id", sId);
            doh.Delete("jcms_normal_skins");
            this._response = JsonResult(1, "成功删除");
        }
        private void ajaxCreateGlobalCSS()
        {
            string _tempcsstext = string.Empty;
            string tempfile = string.Empty;
            JumbotCms.API.PageCSSManage pcm = new JumbotCms.API.PageCSSManage();
            doh.Reset();
            doh.SqlCmd = "Select [ID],[Title],[Source],BodyWidth,LeftWidth,RightWidth,TitleColor,BorderColor,TintBackground,DarkBackground FROM [jcms_normal_skins] ORDER BY id desc";
            DataTable dt = doh.GetDataTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (System.IO.File.Exists(Server.MapPath(site.Dir) + "skins\\" + dt.Rows[i]["Source"].ToString() + "\\_main.css"))
                {
                    pcm.Path = dt.Rows[i]["Source"].ToString();
                    pcm.BodyWidth = Str2Int(dt.Rows[i]["BodyWidth"].ToString());
                    pcm.LeftWidth = Str2Int(dt.Rows[i]["LeftWidth"].ToString());
                    pcm.RightWidth = Str2Int(dt.Rows[i]["RightWidth"].ToString());
                    pcm.TitleColor = dt.Rows[i]["TitleColor"].ToString();
                    pcm.BorderColor = dt.Rows[i]["BorderColor"].ToString();
                    pcm.TintBackground = dt.Rows[i]["TintBackground"].ToString();
                    pcm.DarkBackground = dt.Rows[i]["DarkBackground"].ToString();
                    _tempcsstext = System.IO.File.ReadAllText(Server.MapPath(site.Dir) + "skins\\" + pcm.Path + "\\_main.css");
                    _tempcsstext = _tempcsstext.Replace("{site.Dir}", site.Dir);//替换跟目录路径
                    pcm.Oper(ref _tempcsstext);

                    tempfile = Server.MapPath(site.Dir) + "skins\\" + pcm.Path + "\\css\\global.css";
                    _tempcsstext = "@import url(extends.css);\r\n" + _tempcsstext;
                    SaveCssFile(_tempcsstext, tempfile);
                }
                else
                {
                    this._response = JsonResult(0, "样式方案“" + dt.Rows[i]["Title"].ToString() + "”目录下不存在_main.css");
                    return;
                }
            }
            dt.Clear();
            dt.Dispose();
            this._response = JsonResult(1, "主样式更新完成");
        }
    }
}